<?php
/**
 * Created by b17
 * 2/7/12 9:37 PM
 */

namespace Managers;

use \Managers\Cache\ArrayCache;

class BaseManager extends \Lib\Model\DoctrineManager
{
    protected $__table;

    protected $cacheNames = array();

    protected $limit = null;

    protected $offset = null;

    /**
     * @var \Doctrine\Common\Cache\CacheProvider
     */
    protected $_cache;

    /**
     * @var \Zend_Log
     */
    protected $logger;

    public function __construct()
    {
        parent::__construct();
        $this->logger = \Zend_Registry::get('g4t.log');
        $this->_cache = ArrayCache::getInstance();
    }

    public function find($id)
    {
        return $this->_em->getRepository($this->__table)->findOneBy(array('id' => $id, 'isDel' => 0));
    }

    public function findAll()
    {
        return $this->_em->getRepository($this->__table)->findBy(array('isDel' => 0));
    }

    protected function createQueryBuilder()
    {
        $qb = $this->_em->createQueryBuilder();
        if (isset($this->limit)) {
            $qb->setMaxResults($this->limit);
        }
        if (isset($this->offset)) {
            $qb->setFirstResult($this->offset);
        }
        return $qb;
    }

    public function delete($id)
    {
        $entity = $this->find($id);
        if ($entity) {
            $entity->setIsDel(true);
            $this->_em->persist($entity);
            $this->flush();
        }
        return $entity;
    }

    /**
     * @return \Zend_Cache_Core
     */
    public function _getCacheManager()
    {
        return \Lib\ResLoader::getResource('cachemanager')->getCache('memcached');
    }

    final public function clear()
    {
        foreach ($this->cacheNames as $cacheName) {
            $this->_getCacheManager()->remove($cacheName);
        }
    }

    public function flush($entity = null)
    {
        $this->_em->flush($entity);
        $this->clear();
    }

    public function setLimit($limit)
    {
        $this->limit = $limit;
    }

    public function setOffset($offset)
    {
        $this->offset = $offset;
    }

    public function resetLimit()
    {
        $this->offset = null;
        $this->limit = null;
    }

    protected function _getUploadDir()
    {
        $defaultDir = \Zend_Registry::get('config')->upload->dir;
        $uploadDir = dirname(APPLICATION_PATH) . DIRECTORY_SEPARATOR . $defaultDir . DIRECTORY_SEPARATOR;
        return Utils::sanitizePath($uploadDir);
    }

    protected function _getRealPath($dir)
    {
        $path = dirname(APPLICATION_PATH) . DIRECTORY_SEPARATOR . $dir . DIRECTORY_SEPARATOR;
        return Utils::sanitizePath($path);
    }

    protected function _getUniqueFile($saveDir, $fileName)
    {
        $name = pathinfo($fileName, PATHINFO_FILENAME);
        $ext = pathinfo($fileName, PATHINFO_EXTENSION);
        $count = 0;
        do {
            $fileName = $saveDir . DIRECTORY_SEPARATOR . $name . '_' . $count . '.' . $ext;
            $count++;
        } while (file_exists($fileName));
        return $fileName;
    }

    protected function _setLimit(\Doctrine\ORM\QueryBuilder $qb, $limit = null, $offset = null)
    {
        if (isset($limit)) {
            $qb->setMaxResults($limit);
        }

        if (isset($offset)) {
            $qb->setFirstResult($offset);
        }
    }
}
